package com.hgwz.java;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 利用正则表达式显示验证码短信里的数字
 */
public class Test5RX {

    public static void main(String[] args) {
        String sms = "【哈哈科技】你正在登录哈哈APP，验证码964366。转发可能导致账号被盗。如果这不是你本人操作，回复JZ可阻止该用户登录你的APP。";
        String code = Test5RX.getPassCode(sms);
        System.out.println("The passcode: " + code);
    }

    /**
     * Get the passcode from the sms text.
     *
     * @param sms String: Full text of the sms
     * @return String: The passcode.
     */
    public static String getPassCode(String sms){
        String rg = "(\\d+)";
        Pattern pattern = Pattern.compile(rg, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(sms);
        String code = null;
        if (matcher.find()) {
            code = matcher.group(1);
        }

        return code;
    }
}
